package com.liangtian.mdm.material.common.util;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;

public class ExcelExportUtil {

    public static void outExportExcel(String title, XSSFWorkbook workbook, HttpServletResponse response) {
        OutputStream out = null;
        try {
            String fileName = title.replace(" ", "") + ".xlsx";
            out = response.getOutputStream();
            response.reset();// 清空输出流
            fileName = new String(fileName.getBytes("GBK"), "iso8859-1");
            response.setHeader("Content-disposition", "attachment; filename="
                    + fileName);// 设定输出文件头
            response.setContentType("application/msdownload");// 定义输出类型
            workbook.write(out);
        } catch (IOException e) {
            throw new RuntimeException("导出数据异常");
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                throw new RuntimeException("导出关闭异常");
            } // 关闭流
        }
    }

}
